package com.lijie.bs_recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.WrapperUtil;
import com.lijie.bs_recruit.mapper.UserMapper;
import com.lijie.bs_recruit.model.User;
import com.lijie.bs_recruit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李杰
 * @since 2021-03-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    WrapperUtil wrapperUtil;
    //增加职位

    //修改职位

    //查看职位投递人

    //Hr列表

    /**
     * 登录:
     * 管理员登录方法，admin：判断管理员，rightAdmin：正确管理员
     * @param
     * @return  status: 0成功 ，1用户名错误  2 密码错误
     */
    public Map<String, Object> login(String username,String password) {
        Map<String ,Object> result =new HashMap<>();
        int status=-1;
        //查询条件
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("userName",username);
        //通过username查询
        User rightUser = this.getOne(wrapper);
        if(rightUser!=null){
            //存在用户
            if(!rightUser.getPassword().equals(password)){
                status = 2;//密码错误
            }else{
                status=0;
                result.put("user",rightUser);
            }
        }else {
            status=1;//不存在用户
        }
        result.put("status",status);
        return result;
    }

    //更具用户名查询user

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("userName",username);
        User user1 = userMapper.selectOne(wrapper);
        return user1;
    }

    @Override
    public PageInfo<User> selUserPage(Integer pageIndex, User user) {
        QueryWrapper<User> businessQueryWrapper = wrapperUtil.wrapperUser(user);
        PageHelper.startPage(pageIndex,8);
        List<User> users = baseMapper.selectList(businessQueryWrapper);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
}
